//209.长度最小的子数组
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

int minSubArrayLen(int target,vector<int>& nums){
    //滑动窗口算法
    int left=0;
    int right = 0;

    int sum = 0;
    size_t len=0;
    size_t minlen = -1;

    //右指针进窗口
    while(right<nums.size()){
        sum += nums[right];

        //当和大于目标值时就是左指针出窗口
        if(sum>=target){
            //注意这里一定是左指针小于等于右指针
            while(left<=right&&sum>=target){
                len = right - left + 1;
                if(len<minlen){
                    minlen = len;
                }

                sum -= nums[left];
                left++;
            }
        }

        right++;
    }

    return minlen != -1 ? minlen : 0;
}
int main(){
    int target = 11;
    vector<int> v = {1,1,1,1,1,1,1,1};
    int minlen = minSubArrayLen(target, v);

    cout << minlen;
    return 0;
}